<?php

namespace App;

use Illuminate\Database\Eloquent\Model;
use DB;

class Activity extends Model
{
    public $timestamps = false;
    protected $table = 's_activity';
    //促销种类
    const TYPE_ROB = 3;
    const TYPE_COLLAGE = 2;

    static $TypeDisplay = [
        self::TYPE_ROB => '抢团',
        self::TYPE_COLLAGE => '拼团',
    ];


    /**
     * 修改活动
     * @param $request
     * @param $activity_id
     * @return array
     */
    static function ActivityModify($request, $activity_id, $start_time, $end_time)
    {
        $end_time2 = date('m', $end_time) . ' ' . date('H', $end_time) . ' ' . date('d', $end_time) . ' ' . date('m', $end_time) . ' *';
        $activity = Activity::findOrFail($activity_id);
        $activity->aname = $request->aname;
        $activity->type = $request->type;
        $activity->introduce = $request->introduce;
        $activity->start_time = date('Y-m-d H:i:s', $start_time);
        $activity->end_time = date('Y-m-d H:i:s', $end_time);
        $activity->end_time2 = $end_time2;
        $activity->sunm = $request->sunm;
        $activity->win_num = $request->win_num;

        if (!$activity->save()) {
            return ['status' => 2, 'message' => '修改失败'];
        }

        return ['status' => 1, 'message' => '修改成功'];
    }

    /**
     * 添加活动
     * @param $request
     * @return array
     */
    static function ActivityPublish($request, $start_time, $end_time)
    {
        $end_time2 = date('m', $end_time) . ' ' . date('H', $end_time) . ' ' . date('d', $end_time) . ' ' . date('m', $end_time) . ' *';
        $activity = new Activity();
        $activity->aname = $request->aname;
        $activity->type = $request->type;
        $activity->sunm = $request->sunm;
        $activity->win_num = $request->win_num;
        $activity->introduce = $request->introduce;
        $activity->start_time = date('Y-m-d H:i:s', $start_time);
        $activity->end_time = date('Y-m-d H:i:s', $end_time);
        $activity->end_time2 = $end_time2;//date('m-d H:i', $end_time) . '*';
        if (!$activity->save()) {

            return ['status' => 2, 'message' => '添加失败'];
        }

        //发送一个get请求
        $params = [
            'a' => date('i', $end_time),
            'b' => date('H', $end_time),
            'c' => date('d', $end_time),
            'd' => date('m', $end_time),
            'e' => '*',
            'cxid' => $activity->id
        ];
        $get = Curl::GetCurl(Curl::CURL_URL . Curl::CURL_ACTIVE,$params);
        return ['status' => 1, 'message' => '成功'];
    }


    /**
     * 未到结束时间的活动
     * @return array
     */
    static function AvailableActivity()
    {
        $time = date('Y-m-d H:i:s', time());
        $sql = "select id,aname,introduce from s_activity where end_time > '$time'";
        return DB::select($sql);
    }
}
